package com.hawthorn.mq.listener;

import com.hawthorn.mq.service.syslogjob.SysLogJobService;
import com.hawthorn.mq.service.sysloglogin.SysLogLoginService;
import com.hawthorn.mq.service.syslogoperation.SysLogOperationService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * <p>todo 消息消费监听</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/7/10 09:31
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Component
@Slf4j
public class TopicReceiver
{
  @Autowired
  private SysLogLoginService sysLogLoginService;
  @Autowired
  private SysLogOperationService sysLogOperationService;
  @Autowired
  private SysLogJobService sysLogJobService;
  // @RabbitHandler
  // @RabbitListener(queues = "topic.man")//监听的队列名称 topic.man
  // public void process(Map testMessage)
  // {
  //   log.info("TopicReceiver消费者收到消息  : " + testMessage.toString()
  //   );
  // }
  //
  // @RabbitHandler
  // @RabbitListener(queues = "topic.women")//监听的队列名称 topic.women
  // public void process2(Map testMessage)
  // {
  //   log.info("TopicReceiver消费者收到消息2  : " + testMessage.toString());
  // }

  /**
   * 接收 登录日志
   * @param receiveData
   */
  @RabbitHandler
  @RabbitListener(queues = "hawthorn.topic.logs.login")//监听的队列名称 topic.logs.login
  public void processLoginLog(Map receiveData, Channel channel, Message message) throws IOException
  {
    try
    {
      log.info("TopicReceiver消费者收到消息  : " + receiveData.toString());
      sysLogLoginService.saveLogLogin(receiveData);
      channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    } catch (Exception e)
    {
      e.printStackTrace();
      channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
      log.error(e.getMessage());
    }

  }

  /**
   * 接收 操作日志
   * @param receiveData
   * @param userAccount
   */
  @RabbitHandler
  @RabbitListener(queues = "hawthorn.topic.logs.operation")//监听的队列名称
  public void processOperationLog(Map receiveData, Channel channel, Message message) throws IOException
  {
    try
    {
      log.info("TopicReceiver消费者收到消息  : " + receiveData.toString());
      sysLogOperationService.saveLogOperation(receiveData);
      channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    } catch (Exception e)
    {
      e.printStackTrace();
      channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
      log.error(e.getMessage());
    }

  }

  /**
   * 接收 job日志
   */
  @RabbitHandler
  @RabbitListener(queues = "hawthorn.topic.logs.job")//监听的队列名称
  public void processJobLog(Map receiveData, Channel channel, Message message) throws IOException
  {
    try
    {
      log.info("TopicReceiver消费者收到消息  : " + receiveData.toString());
      sysLogJobService.saveLogJob(receiveData);
      channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    } catch (Exception e)
    {
      e.printStackTrace();
      channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
      log.error(e.getMessage());
    }

  }
}
